/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 * 	   IBM Corporation - initial API and implementation for JDT/DLTK
 *     Sean W. Quinn - adoption for use with PHP from various sources.
 */
package org.eclipse.php.core;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.dltk.core.ModelException;

/**
 * A checked exception representing a failure in the PHP model. PHP model
 * exceptions contain a PHP-specific status object describing the cause of the
 * exception.
 * <p>
 * Instances of this class are automatically created by the PHP model when
 * problems arise, so there is generally no need for clients to create
 * instances.
 * </p>
 *
 * @see IPHPModelStatus
 * @see IPHPModelStatusConstants
 *
 * @noextend This class is not intended to be subclassed by clients.
 */
public class PHPModelException extends ModelException {

	/** The Serial UID */
	private static final long serialVersionUID = 4366499569237932556L;

	/**
	 * {@inheritDoc}
	 */
	public PHPModelException(Throwable e, int code) {
		super(e, code);
	}

	/**
	 * {@inheritDoc}
	 */
	public PHPModelException(final CoreException exception) {
		super(exception);
	}

	/**
	 * {@inheritDoc}
	 */
	public PHPModelException(final ModelException exception) {
		super(exception);
	}

	/**
	 * Creates a PHP model exception for the given PHP-specific status object.
	 *
	 * @param status
	 *            the PHP-specific status object
	 */
	public PHPModelException(IPHPModelStatus status) {
		super(status);
	}

	/**
	 * Returns the PHP model status object for this exception. Equivalent to
	 * <code>(IPHPModelStatus) getStatus()</code>.
	 *
	 * @return the status object cast to a {@code IPHPModelStatus}.
	 */
	public IPHPModelStatus getPHPModelStatus() {
		return (IPHPModelStatus) getModelStatus();
	}

	/*
	 * Returns a printable representation of this exception suitable for
	 * debugging purposes only.
	 */
	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("PHP Model Exception: "); //$NON-NLS-1$
		if (getException() != null) {
			if (getException() instanceof CoreException) {
				CoreException c = (CoreException) getException();
				buffer.append("Core Exception [code "); //$NON-NLS-1$
				buffer.append(c.getStatus().getCode());
				buffer.append("] "); //$NON-NLS-1$
				buffer.append(c.getStatus().getMessage());
			} else {
				buffer.append(getException().toString());
			}
		} else {
			buffer.append(getStatus().toString());
		}
		return buffer.toString();
	}
}
